package co.fitstart.coach.web.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import co.fitstart.coach.entity.security.Login;
import co.fitstart.coach.service.CourseHourService;
import co.fitstart.coach.service.PrincipalService;
import co.fitstart.coach.web.resolver.Secure;
import co.fitstart.entity.coach.CoachSigninRecord;
import co.fitstart.entity.coach.CoachSigninStatistic;
import co.fitstart.entity.principal.Principal;

@Controller
@RequestMapping("/hours")
public class CourseHourControlller {
    
    @Autowired
    private PrincipalService principalService;
    
    @Autowired
    private CourseHourService courseHourService;
    
    /**
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(@Secure Login login){
        
        ModelAndView modelAndView = new ModelAndView("hour/hour-index");
        
        Principal coach = principalService.load(Principal.class, login.getId());
        
        // list monthly course hour
        List<CoachSigninStatistic> statistics = courseHourService.listCoachSigninStatistic(coach);
        Map<String, BigDecimal> hideStatistics = courseHourService.listCoachHideStatistic(coach);
        
        modelAndView.addObject("statistics", statistics);
        modelAndView.addObject("experiences", hideStatistics);
        
        return modelAndView;
    }
    
    /**
     * List salaries by month, including monthly salary report and monthly salary detail
     * 
     * @param id
     * @param month
     * @return
     */
    @RequestMapping(value = "/{statisticId}", method = RequestMethod.GET)
    public ModelAndView listByMonth(@Secure(property = "id") Long id, @PathVariable("statisticId") Long statisticId){
        
        ModelAndView modelAndView = new ModelAndView("hour/monthly-hour");
        
        Principal coach = principalService.load(Principal.class, id);
        
        CoachSigninStatistic statistic = courseHourService.loadCoachSigninStatistic(statisticId);
        BigDecimal hideHour = courseHourService.getCoachHideStatistic(coach, statistic.getMonth());
        
        List<CoachSigninRecord> records = courseHourService.listCoachSigninRecord(coach, statistic.getMonth());
        
        modelAndView.addObject("statistic", statistic);
        modelAndView.addObject("experienceHour", hideHour);
        modelAndView.addObject("records", records);
        
        return modelAndView;
    }

}
